<?php
class Installer extends DbConnector {
	private static $sqlFile = "podcastproject.sql";
	private static $sessionToken = "podcastproject_db";
	
	public function checkIfInstalled() {
		// only check, if it has not already been done
		if(!isset($_SESSION[self::$sessionToken]) || !$_SESSION[self::$sessionToken]) {
			$sqlContent = file_get_contents(self::$sqlFile);
			
			// get required table names
			preg_match_all("#EXISTS (`.*`)#", $sqlContent, $matches);

			$query = "SELECT * FROM ";
			foreach ($matches[1] as $tablename) {
				$query .= "$tablename, ";
			}
			$query .= "LIMIT 0,1";
			$query = str_replace(", LIMIT", " LIMIT", $query);
			
			$res = self::$db->query($query);
			
			if(!$res) {
				$queries = explode(";", $sqlContent);
				
				foreach ($queries as $query) {
					self::$db->query($query);
				}
				
				echo "Database tables created.";
				die();
			}

			// everything is just fine
			$_SESSION[self::$sessionToken] = true;
		}
	}
}

?>
